//
//  LocalizeManager.m
//  Unity-iPhone
//
//  Created by Karl on 2017/3/28.
//
//

#import "LocalizeManager.h"

@implementation LocalizeManager

+ (instancetype) sharedManager{
    static LocalizeManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[LocalizeManager alloc] init];
    });
    return instance;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        _isCN = [[self getPreferredLanguage] hasPrefix:@"zh-Hans"]|| [[self getPreferredLanguage] hasPrefix:@"zh-Hant"]|| [[self getPreferredLanguage] hasPrefix:@"zh-HK"]|| [[self getPreferredLanguage] hasPrefix:@"zh-TW"]?YES:NO;
    }
    return self;
}

- (NSString *)getPreferredLanguage{
    NSUserDefaults *uDefs = [NSUserDefaults standardUserDefaults];
    NSArray *languages = [uDefs objectForKey:@"AppleLanguages"];
    NSString *preferredLanguage = [languages firstObject];
    NSLog(@"preferredLanguage is %@",preferredLanguage);
    return preferredLanguage;
}

- (void)setUnityLanguage{
    
    NSString *info;
    if (_isCN) {
        info = @"{\"language\":\"chinese\"}";
        
    }else{
        info = @"{\"language\":\"english\"}";

    }
    UnitySendMessage("_sceneManager", "SetSystemLanguage", info.UTF8String);

}

@end
